<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情</title>
    <link rel="stylesheet" href="./Css/common.css">
    <link rel="stylesheet" href="./Css/blog_detail.css">

    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />


</head>
<body>
    <div class="nav">
        <img src="./image/西南石油大学.jpeg" alt="">
        <span>我的博客系统</span>
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    <div class="container">
        <div class="left">
            <div class="card">
                <img src="./image/图片2.jpeg" alt="">
                <h3></h3>
                <a href="https://gitee.com/wen-junhao000">github地址</a>
                <div class="couter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="couter">
                    <span>1</span>
                    <span>2</span>
                </div>
            </div>
        </div>
        <div class="right">
            <div class="blog-content">
                <h3></h3>
                <div class="date"></div>
                <div id="content" style="background-color: transparent;"></div>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script>
            $.ajax({
            method:'GET',
            url:'blog'+location.search,
            success:function(data,status){
                buildBlog(data);
            },
            error:function(data,status){
                location.assign('login.html');
            }
        });
        function buildBlog(blog){
            let titleDiv=document.querySelector('.blog-content>h3');
            titleDiv.innerHTML=blog.title;
            let dateDiv=document.querySelector('.blog-content>.date');
            dateDiv.innerHTML=formatDate(blog.postTime);
            // let contentDiv=document.querySelector('.blog-content>.content')
            // contentDiv.innerHTML=blog.content;
            editormd.markdownToHTML('content',{markdown: blog.content});
        } 
        function formatDate(timeStampMS){
        var date = new Date(timeStampMS);

	var year = date.getFullYear(),
		month = date.getMonth()+1,//月份是从0开始的
		day = date.getDate(),
		hour = date.getHours(),
		min = date.getMinutes(),
		sec = date.getSeconds();
	var newTime = year + '-' +
				(month < 10? '0' + month : month) + '-' +
				(day < 10? '0' + day : day) + ' ' +
				(hour < 10? '0' + hour : hour) + ':' +
				(min < 10? '0' + min : min) + ':' +
				(sec < 10? '0' + sec : sec);

	return newTime;
    }
    $.ajax({
        method:'GET',
        url:'user'+location.search,
        success:function(data,status){
            changUser(data);
        }
    })
    function changUser(user){
        let h3=document.querySelector('.card>h3');
        h3.innerHTML=user.username;
        if(user.isYourBlog){
            let navDiv=document.querySelector('.nav');
            let delButton=document.createElement('a');
            delButton.innerHTML='删除';
            delButton.herf='#';
            delButton.onclick=deleteBlog;
            navDiv.appendChild(delButton);
        }
    }
    function deleteBlog(){
        $.ajax({
            url:'blog'+location.search,
            method:'DELETE',
            success:function(data,status){
                location.assign('blog_list.html');
            }
        })
    }
    </script>
    
</body>
</html>